package com.dailyyoga.res;

import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetFileDescriptor;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.util.Log;

class YogaPlugAppRes extends YogaRes {

	Context mContext;

	public YogaPlugAppRes(Context context, String lang) {

		mContext = context;
		mLang = lang;
	}

	public String getString(String plugName, String key) {

		Context context = getPlugContext(plugName);

		return context.getResources().getString(
				context.getResources().getIdentifier(key, "string",
						context.getPackageName()));

	}

	private Context getContext(String plugId) {

		Context mOtherContext = null;

		try {

			mOtherContext = mContext.createPackageContext(plugId,
					Context.CONTEXT_IGNORE_SECURITY
							| Context.CONTEXT_INCLUDE_CODE);

		} catch (NameNotFoundException e) {
		}

		return mOtherContext;
	}

	private Context getPlugContext(String plugId) {
		Context context = getContext(plugId);
		setLang(context);
		return context;
	}

	public Bitmap getBitMap(String plugName, String key) {

		Bitmap bitmap = null;

		try {
			InputStream inputStream = (InputStream) getPlugContext(plugName)
					.getResources().getAssets().open(key);
			bitmap = BitmapFactory.decodeStream(inputStream);
			inputStream.close();
		} catch (Exception e) {
		}

		return bitmap;
	}

	public FileDescriptorEx getPlayAudioPath(String plugName, String key) {

		AssetFileDescriptor descriptor = null;
		try {

			String lgName = "";

			if (mLang.contains("zh")) {
				lgName = key.replace("ogg/", "ogg-" + "zh" + "/");
			} else if (mLang.contains("ja")) {
				lgName = key.replace("ogg/", "ogg-" + "ja" + "/");
			} else if (mLang.contains("ko")) {
				lgName = key.replace("ogg/", "ogg-" + "ko" + "/");
			}

			Log.d("playAudio", lgName + " key " + key);

			descriptor = getPlugContext(plugName).getAssets().openFd(lgName);

		} catch (Exception e) {

			e.printStackTrace();
		}

		if (descriptor == null) {
			try {
				descriptor = getPlugContext(plugName).getAssets().openFd(key);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		FileDescriptorEx descriptorEx = new FileDescriptorEx();
		descriptorEx.mFileDescriptor = descriptor.getFileDescriptor();
		descriptorEx.mStart = descriptor.getStartOffset();
		descriptorEx.mLength = descriptor.getLength();

		return descriptorEx;
	}

	public FileDescriptorEx getVideoPath(String plugName, String key) {

		try {
			AssetFileDescriptor descriptor = getPlugContext(plugName)
					.getAssets().openFd(key);

			FileDescriptorEx descriptorEx = new FileDescriptorEx();
			descriptorEx.mFileDescriptor = descriptor.getFileDescriptor();
			descriptorEx.mStart = descriptor.getStartOffset();
			descriptorEx.mLength = descriptor.getLength();

			return descriptorEx;
		} catch (Exception e) {
			return null;
		}

	}

	public boolean isInstallPlugs(String plugName) {

		return getContext(plugName) != null;
	}

	public InputStream getParamXMl(String plugName, String praramKey)
			throws IOException {
		return getPlugContext(plugName).getResources().getAssets()
				.open(praramKey);
	}

	private void setLang(Context context) {

		Resources resources = context.getResources();// 获得res资源对象

		Configuration config = resources.getConfiguration();// 获得设置对象

		DisplayMetrics dm = resources.getDisplayMetrics();// 获得屏幕参数：主要是分辨率，像素等

		if (mLang.equals("zh_CN_")) {

			config.locale = Locale.SIMPLIFIED_CHINESE;

		} else if (mLang.equals("zh_TW_")) {
			config.locale = Locale.TRADITIONAL_CHINESE;

		} else if (mLang.equals("ja_JP_")) {

			config.locale = new Locale("ja");

		} else if (mLang.equals("en_")) {

			config.locale = new Locale("en");
		}

		resources.updateConfiguration(config, dm);

	}

	public int getPlugVc(String plugName) {
		try {
			PackageManager packageManager = mContext.getPackageManager();
			PackageInfo pi = packageManager.getPackageInfo(plugName, 0);
			return pi.versionCode;
		} catch (Exception e) {
		}
		return -1;

	}

	public Bitmap getPrompt(String plugName, String key) {
		try {
			InputStream inputStream = getPlugContext(plugName).getAssets()
					.open(key);
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
			inputStream.close();
			return bitmap;
		} catch (Exception e) {

			return null;
		}

	}

}
